<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class StatisticsList extends AdminController {

	function __construct() {
		parent::__construct();
		$this -> sidebar = "Overview-Home";
		$this -> load-> model('Student_feedback_model');
		$this -> load-> model('Supervisor_feedback_model');
	}

	function Index() {	
		$this ->load->model('Admin_model');
		$result['dept_list']=$this ->Admin_model->query('select distinct dept_id,dept_name from db_admin where dept_id is not null');	
		$role=explode(',',getSess()['sub_role_id']);
		array_push($role,getSess()['role_id']);

		if(in_array(WebRole::Admin[XPHEnum::Value], $role)  || in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::SchoolLeader[XPHEnum::Value], $role) ){
			$result['dept']=true;
		}else{
			$result['dept']=false;
		}
		$this->load->view('admin/statisticsList',$result);
	}    
    

	function getDatas(){	
		$where='where 1=1';
		$params= $_POST;
		if(isset($params['start_time']) && isset($params['end_time'])){
			$where.=$this ->sqlEscape(' and created>?? and created<??',array(strtotime($params['start_time']),strtotime($params['end_time'])));
		}
		if(isset($params['param'])){
			$where.=$this ->sqlLikeEscape(" and dept_name like '%??%'",array($params['param']));
		}else{
			$where.=$this ->sqlLikeEscape(" and dept_name like '%??%'",array(getSess()['dept_name']));
		}
		if(isset($params['pageNo']) && isset($params['pageSize'])){			
			$pageNo=$_POST['pageNo']-1;
			$pageSize=$_POST['pageSize'];
		}else{
			$pageNo='0';
			$pageSize='20';
		}
		$sql='select * from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback '.$where.' limit '.$pageSize*$pageNo.','.$pageSize;
		$data['ret']=$this -> Student_feedback_model -> query('select * from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback '.$where.' order by created desc'.' limit '.$pageSize*$pageNo.','.$pageSize);
		$count=$this -> Student_feedback_model -> query('select count(*) as count from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback '.$where);
		$data['count']=$count[0]['count'];
		// $result['']
		$data['totalPage']=ceil($data['count']/$pageSize);
		$data['pageNo']=$_POST['pageNo'];

		foreach ($data['ret'] as $k => $v) {
			$data['ret'][$k]['created']=date('Y-m-d H:i:s',$v['created']);
			$data['ret'][$k]['type']=(new SupervisorFeedbackType)->show($v['type']);
		}
		// exit(retJson('返回反馈列表',true,$result));
		echo json_encode($data);
	}

	function Export1($dept_name,$start_time,$end_time){
		// $data=$_POST;
		// $result=$data['start_time'].'-'.$data['end_time'].'<br>';
		// $result.=
		$where='where 1=1';
		$params= $_POST;
		if(isset($params['start_time']) && isset($params['end_time'])){
			$where.=$this ->sqlEscape(' and created>?? and created<??',array(strtotime($params['start_time']),strtotime($params['end_time'])));
		}
		if(isset($params['pageNo']) && isset($params['pageSize'])){			
			$pageNo=$_POST['pageNo']-1;
			$pageSize=$_POST['pageSize'];
		}else{
			$pageNo='0';
			$pageSize='20';
		}
		$sql='select * from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback '.$where.' limit '.$pageSize*$pageNo.','.$pageSize;
		$data['ret']=$this -> Student_feedback_model -> query('select * from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback '.$where.' order by created desc'.' limit '.$pageSize*$pageNo.','.$pageSize);
		if(!isset($params['start_time']) && !isset($params['end_time'])){
			$end_time=$this -> Student_feedback_model -> query('select max(created) end_time from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback '.$where.' order by created desc'.' limit '.$pageSize*$pageNo.','.$pageSize);
			$start_time=$this -> Student_feedback_model -> query(' select min(created) start_time from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback '.$where.' order by created desc'.' limit '.$pageSize*$pageNo.','.$pageSize);
			var_dump($end_time);
			$params['end_time']=strtotime('Y-m-d',$end_time[0]['end_time']);
			$params['start_time']=strtotime('Y-m-d',$start_time[0]['start_time']);
		}
		foreach ($data['ret'] as $k => $v) {
			
		}
		$result=$params['start_time'].'-'.$params['end_time'].'<br>'.$params['param'];
		
		$this -> load -> view("admin/statisticsFeedBackExport",$result);
	}

	function Export($dept_id,$start_time,$end_time){			
			$where=$this->sqlEscape('where feedback.status<>?? and feedback.del_flag=??',array(FeedBackStatus::ToAudit[XPHEnum::Value],CommonStatus::UnDeleted[XPHEnum::Value]));
			$params= $_POST;
			if(isset($start_time) && isset($end_time)){
				$where.=$this ->sqlEscape(' and feedback.created>?? and feedback.created<??',array(strtotime($start_time),strtotime($end_time)));
			}	
			if(isset($dept_id)){
				$where.=$this ->sqlEscape(' and dept_id=??',array($dept_id));
			}		
			$data['ret']=$this -> Student_feedback_model -> query("select date_format(from_unixtime(feedback.created),'%Y-%m-%d') as day from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback left join db_admin on db_admin.dept_name=feedback.dept_name ".$where." group by day");
			// if(!isset($start_time) && !isset($end_time)){
			// 	$end_time=$this -> Student_feedback_model -> query('select max(created) end_time from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback '.$where.' order by created desc'.' limit '.$pageSize*$pageNo.','.$pageSize);
			// 	$start_time=$this -> Student_feedback_model -> query(' select min(created) start_time from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback '.$where.' order by created desc'.' limit '.$pageSize*$pageNo.','.$pageSize);
			// 	$end_time=strtotime('Y-m-d',$end_time[0]['end_time']);
			// 	$start_time=strtotime('Y-m-d',$start_time[0]['start_time']);
			// }			
			$dept_name=$this -> Admin_model -> get_single(array('dept_id'=>$dept_id));
			$result['data']['start_time']=$start_time;
			$result['data']['end_time']=$end_time;
			$result['data']['dept_name']=$dept_name['dept_name'];
			$result['data']['data']=array();
			foreach ($data['ret'] as $k => $v) {
				if(!isset($result['data']['data'][$k])){
					$result['data']['data'][$k]=array('time'=>$v['day'],'data'=>array());
				}else{
					array_push($result['data']['data'][$k]['time'],array('time'=>array(date('Y-m-d',$v['day']))));
				}
				$time_data=$this -> Student_feedback_model->query("select content,result from(select * from db_student_feedback union all select * from db_supervisor_feedback) feedback where date_format(from_unixtime(created),'%Y-%m-%d')='".$v['day']."' and status<>".FeedBackStatus::ToAudit[XPHEnum::Value]." and del_flag=".CommonStatus::UnDeleted[XPHEnum::Value]);
				foreach ($time_data as $k1 => $v1) {
					array_push($result['data']['data'][$k]['data'],array('content'=>$v1['content'],'result'=>$v1['result']));					
				}

			}
			
			$this -> load -> view("admin/statisticsFeedBackExport",$result);
		}
	
	

}
